package com.car.loan.project.xr.controller;

import com.car.loan.common.exception.loan.LoanParameterException;
import com.car.loan.framework.aspectj.lang.annotation.DataStatus;
import com.car.loan.framework.aspectj.lang.annotation.Log;
import com.car.loan.framework.aspectj.lang.enums.BusinessType;
import com.car.loan.framework.web.domain.AjaxResult;
import com.car.loan.project.xr.domain.LoanStatus;
import com.car.loan.project.xr.service.ILoanService;
import com.car.loan.project.xr.service.ILoanStatusService;
import com.car.loan.project.xr.util.bo.business.LoanBaseInfo;
import com.car.loan.project.xr.util.enums.BusinessApostilleStatus;
import com.car.loan.project.xr.util.enums.CarInfoStatus;
import com.car.loan.project.xr.util.enums.DataAuditStatus;
import com.car.loan.project.xr.util.enums.LoanBusinessAuditStatus;
import com.car.loan.project.xr.util.enums.PaymentApplyStatus;
import com.car.loan.project.xr.util.select.LoanApplySelect;
import com.car.loan.project.xr.util.vo.BusinessAuditDetailVo;
import com.car.loan.project.xr.util.vo.BusinessAuditInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.Optional;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author huangding
 * @description
 * @date 2019/11/26 16:08
 */
@RestController
@RequestMapping("xr/loan/apostille")
@Api(value = "加签审核", tags = "车贷业务-加签审核")
public class LoanApostilleController {

    @Autowired
    private ILoanStatusService loanStatusService;


    @Autowired
    private ILoanService loanService;

    @PreAuthorize("@ss.hasPermi('loanaudit:apostille:query')")
    @GetMapping("list")
    @ApiOperation(value = "获取业务加签审核列表", response = LoanBaseInfo.class, responseContainer = "list")
    @DataStatus(loanStatusEnum = com.car.loan.common.enums.LoanStatusEnum.BUSINESS_APOSTILLE_STATUS)
    public AjaxResult applyList(@Valid LoanApplySelect loanApplySelect) {
        return AjaxResult.success(loanService.page(loanApplySelect));
    }


    @PreAuthorize("@ss.hasPermi('loanaudit:apostille:query')")
    @GetMapping("/{loanId}")
    @ApiOperation(value = "获取业务加签审核详情", notes = "获取业务加签审核详情", response = BusinessAuditDetailVo.class)
    public AjaxResult getApostilleInfo(@PathVariable Long loanId) {
        return AjaxResult.success(loanService.getBusinessAuditDetailVo(loanId));
    }

    @PreAuthorize("@ss.hasPermi('loanaudit:apostille:audit')")
    @PostMapping()
    @Log(title = "业务审核", businessType = BusinessType.INSERT)
    @ApiOperation("业务审核")
    public AjaxResult apostilleAudit(@Valid @RequestBody BusinessAuditInfo businessAuditInfo) {
        return loanService.apostilleAudit(businessAuditInfo);
    }


    @PreAuthorize("@ss.hasPermi('loanaudit:apostille:withdraw')")
    @PutMapping("withdraw/{loanId}")
    @Log(title = "撤回业务审核", businessType = BusinessType.WITHDRAW)
    @ApiOperation(value = "撤回业务审核", notes = "车辆信息，付款申请，资料审核通过不予撤回")
    public AjaxResult withdrawApply(@PathVariable Long loanId) {
        Optional<LoanStatus> optionalLoanStatus = loanStatusService.getByLoanId(loanId);
        LoanStatus loanStatus = optionalLoanStatus
            .orElseThrow(() -> new LoanParameterException("业务状态数据异常请联系管理员"));
        Long carInfoStatus = loanStatus.getCarInfoStatus();
        Long dataAuditStatus = loanStatus.getDataAuditStatus();
        Long paymentApplyStatus = loanStatus.getPaymentApplyStatus();
        if (CarInfoStatus.SUBMIT.status == carInfoStatus
            || DataAuditStatus.SUBMIT.status == dataAuditStatus
            || PaymentApplyStatus.SUBMIT.status == paymentApplyStatus) {
            return AjaxResult.error("当前状态不可撤回");
        }
        loanStatus.setBusinessApostilleStatus((long) BusinessApostilleStatus.INIT.status);
        loanStatus.setBusinessAuditStatus((long) LoanBusinessAuditStatus.APOSTILLE.status);
        loanStatusService.updateLoanStatus(loanStatus);
        return AjaxResult.success();
    }

}
